#include "findpasswordform.h"
#include "ui_findpasswordform.h"
#include "widget.h"
#include <QDebug>
#include <QTimer>

findPasswordForm::findPasswordForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::findPasswordForm)
{
    ui->setupUi(this);
    m_logManager = new LogManager(this);
    //连接点击btnRegister信号和槽
    connect(ui->btnFindPassword,&QPushButton::clicked,this,[=](){
        if(ui->linePhoneOrMile->text().isEmpty() ){
            emit sendMessage("输入手机号或邮箱为空，请输入有效内容!");
        }else if(ui->lineNewPassword->text().isEmpty() ){
            emit sendMessage("密码为空，请输入有效内容!");
        }else if(ui->lineNewPasswordAgain->text().isEmpty() ){
            emit sendMessage("邮箱为空，请输入有效内容!");
        }else{
            userInfoByPhone = m_userMgr.queryUserByUsernamePhoneMail("",ui->linePhoneOrMile->text(),"");
            userInfoByMail = m_userMgr.queryUserByUsernamePhoneMail("","",ui->linePhoneOrMile->text());

            // 检查是否查询到用户信息
            if (!userInfoByPhone.isEmpty()||!userInfoByMail.isEmpty()) {
                if(!userInfoByPhone.isEmpty()){
                    if(ui->lineNewPassword->text() != ui->lineNewPasswordAgain->text()){
                        emit sendMessage("密码不一致!");
                    }else{
                        QString tempUsername = userInfoByPhone["username"].toString();
                        if (m_userMgr.updateByUsername(tempUsername, "", ui->lineNewPasswordAgain->text(), "", "", "")) {
                            emit display(Widget::INDEX_MAINBAR);
                            // 记录操作日志
                            if (m_logManager) {
                                m_logManager->recordSystemLog(
                                            QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"),
                                            "OPERATION",                                     // 日志类型
                                            "INFO",                                          // 日志级别
                                            QString("用户[%1]修改密码成功!").arg(tempUsername),   //操作内容
                                            userInfoByPhone["user_id"].toInt(),               // 用户ID
                                            -1                                               // 设备ID
                                        );
                            }

                            // 清空输入框
                            ui->lineNewPassword->clear();
                            ui->linePhoneOrMile->clear();
                            ui->lineNewPasswordAgain->clear();
                        } else {
                            emit sendMessage("错误:"+m_userMgr.getLastError());
                            // 记录操作日志
                            if (m_logManager) {
                                m_logManager->recordSystemLog(
                                            QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"),
                                            "OPERATION",                                     // 日志类型
                                            "INFO",                                          // 日志级别
                                            QString("用户[%1]修改密码失败!").arg(tempUsername),   //操作内容
                                            userInfoByPhone["user_id"].toInt(),               // 用户ID
                                            -1                                               // 设备ID
                                        );
                            }
                        }
                    }
                }
                if(!userInfoByMail.isEmpty()){
                    if(ui->lineNewPassword->text() != ui->lineNewPasswordAgain->text()){
                        emit sendMessage("密码不一致!");
                    }else{
                        QString tempUsername = userInfoByMail["username"].toString();
                        if (m_userMgr.updateByUsername(tempUsername, "", ui->lineNewPasswordAgain->text(), "", "", "")) {
                            emit display(Widget::INDEX_MAINBAR);
                            if (m_logManager) {
                                m_logManager->recordSystemLog(
                                            QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"),
                                            "OPERATION",                                     // 日志类型
                                            "INFO",                                          // 日志级别
                                            QString("用户[%1]修改密码成功!").arg(tempUsername),   //操作内容
                                            userInfoByMail["user_id"].toInt(),               // 用户ID
                                             -1                                               // 设备ID
                                        );
                            }
                            // 清空输入框
                            ui->lineNewPassword->clear();
                            ui->linePhoneOrMile->clear();
                            ui->lineNewPasswordAgain->clear();
                        } else {
                            emit sendMessage("错误:"+m_userMgr.getLastError());
                            if (m_logManager) {
                                m_logManager->recordSystemLog(
                                            QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"),
                                            "OPERATION",                                     // 日志类型
                                            "INFO",                                          // 日志级别
                                            QString("用户[%1]修改密码失败!").arg(tempUsername),   //操作内容
                                            userInfoByMail["user_id"].toInt(),               // 用户ID
                                            -1                                               // 设备ID
                                        );
                            }
                        }
                    }
                }
            } else {
                // 若未查询到，输出错误信息
                emit sendMessage("错误:"+m_userMgr.getLastError());
            }
        }
    });

    //连接点击btnGoLogin信号和发送display登录界面(0)信号
    connect(ui->btnGoLogin,&QPushButton::clicked,this,[=](){
        emit display(Widget::INDEX_MAINBAR);
    });
}

findPasswordForm::~findPasswordForm()
{
    delete ui;
}

